/**
 * 
 */
package main.org.form.action;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.logging.Level;

import org.adempiere.exceptions.DBException;
import org.compiere.model.MBPMForm;
import org.compiere.model.MBPMProject;
import org.compiere.model.MBPMSubs;
import org.compiere.model.MBPMVersionBudget;
import org.compiere.model.MOrg;
import org.compiere.model.MPeriod;
import org.compiere.model.X_BPM_VersionBudget;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

import main.org.model.Budget;
import main.org.model.Utils;

/**
 * @author V.Sokolov
 * 
 */
public class Forms extends Budget {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8168009313401256223L;

	public static final String sessionKey = "forms";
	protected String page;
	protected MBPMForm[] formsLine;
	protected int formID;
	protected int BPM_VersionBudget_ID;
	protected int BPM_Project_ID;
	protected int AD_PInstance_ID;
	protected int AD_User_ID;
	protected int formCodeID;
	protected int AD_Client_ID;
	protected int AD_Org_ID;
	X_BPM_VersionBudget budget;
	MOrg org;
	LinkedHashMap<Integer, String> projectList = new LinkedHashMap<Integer, String>();
	private int defaultProjectID;

	/* 
	 */
	@Override
	public String execute() throws Exception {

		setPage(Utils.TEMPLATE_FIRST);
		StringBuilder err = new StringBuilder();
		System.out.println("AD_PInstance_ID: " + AD_PInstance_ID);
				
		if (AD_PInstance_ID > 0) {
			String sql = "SELECT a.AD_Client_ID, a.AD_User_ID,b.ParameterName, b.P_Number \n"
					+"FROM AD_PInstance AS a \n"
					+"LEFT JOIN AD_PInstance_Para AS b ON b.AD_PInstance_ID=a.AD_PInstance_ID \n"
					+"WHERE a.ad_pinstance_id="+AD_PInstance_ID;

			PreparedStatement pstmt = null;
			ResultSet rs = null;
			try {
				pstmt = DB.prepareStatement(sql, null);
				rs = pstmt.executeQuery();
				while (rs.next()) {
					AD_Client_ID = rs.getInt("AD_Client_ID");
					AD_User_ID = rs.getInt("AD_User_ID");
					String sParamName=rs.getString("ParameterName");
					if ("BPM_VersionBudget_ID".equals(sParamName)) 
						BPM_VersionBudget_ID=rs.getInt("P_Number");
					else if ("AD_Org_ID".equals(sParamName)) 
						AD_Org_ID=rs.getInt("P_Number");
					else if ("BPM_Project_ID".equals(sParamName)) 
						BPM_Project_ID=rs.getInt("P_Number");
				}
			} catch (SQLException e) {
				CLogger.get().log(Level.INFO, sql, e);
				throw new DBException(e, sql);
			} finally {
				DB.close(rs, pstmt);
				rs = null;
				pstmt = null;
			}
		} else
			err.append("Нет форм с настройками на данный бюджетный календарь <br />");
		Env.setContext(Env.getCtx(), "#AD_Client_ID", AD_Client_ID);
		Env.setContext(Env.getCtx(), "#AD_Org_ID", AD_Org_ID);
		Env.setContext(Env.getCtx(), "#AD_Language", getLang());
		Env.setContext(Env.getCtx(), "#AD_User_ID", AD_User_ID);

		System.out.println("BPM_VersionBudget_ID:	" + BPM_VersionBudget_ID + ", AD_Org_ID: " +AD_Org_ID);
		formsLine = MBPMForm.getForms(BPM_VersionBudget_ID,AD_Org_ID);

		if (formsLine.length == 0)
			err.append("Нет форм с настройками на данный бюджетный календарь <br />");

		setBudget();

		if (BPM_VersionBudget_ID == 0)
			err.append("Не найден текущий бюджетный календарь на год <br />");

		setProjectList();
		if (getProjectList().size() == 0)
			err.append("Не найден рабочий проект <br />");

		if (AD_Org_ID == 0) {
			err.append("Неверно настроен справочник Орг. единиц <br />");
			if (AD_User_ID == 0)
				err.append("Не найден ID пользователя или головная организация<br />");
		}
		setOrg(new MOrg(Env.getCtx(),AD_Org_ID,null));
		setMessageError(err.toString());
		if (err.length() == 0) {
			session.put(Forms.sessionKey, this);
			return SUCCESS;
		} else
			return NONE;

	}

	public X_BPM_VersionBudget getBudget() {
		return budget;
	}

	public int getDefaultProjectID() {
		return defaultProjectID;
	}

	public int getFormCodeID() {
		return formCodeID;
	}

	public int getFormID() {
		return formID;
	}

	public MBPMForm[] getFormsLine() {
		return formsLine;
	}

	public String getPage() {
		return page;
	}

	public LinkedHashMap<Integer, String> getProjectList() {
		return projectList;
	}

	public MOrg getOrg() {
		return org;
	}

	public int getAD_Org_ID() {
		return AD_Org_ID;
	}

	public int getBPM_VersionBudget_ID() {
		return BPM_VersionBudget_ID;
	}

	private void loadProjects() {
		MBPMProject project = new MBPMProject(Env.getCtx(), BPM_Project_ID, null);
		this.projectList.put(BPM_Project_ID, project.getName());
		setDefaultProjectID(BPM_Project_ID);
	}

	public void setBudget() {
		this.budget = new MBPMVersionBudget(Env.getCtx(), BPM_VersionBudget_ID, null);
	}

	public void setDefaultProjectID(int defaultProjectID) {
		this.defaultProjectID = defaultProjectID;
	}

	public void setFormCodeID(int formCodeID) {
		this.formCodeID = formCodeID;
	}

	public void setFormID(int formID) {
		this.formID = formID;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public void setProjectList() {
		loadProjects();
	}

	public void setOrg(MOrg org) {
		this.org = org;
	}

	public void setBPM_VersionBudget_ID(int BPM_VersionBudget_ID) {
		this.BPM_VersionBudget_ID = BPM_VersionBudget_ID;
	}

	public int getAD_PInstance_ID() {
		return AD_PInstance_ID;
	}

	public void setAD_PInstance_ID(int AD_PInstance_ID) {
		this.AD_PInstance_ID = AD_PInstance_ID;
	}

	public void setOrgID(int orgID) {
		this.AD_Org_ID = orgID;
	}

	public int getOrgID() {
		return AD_Org_ID;
	}

	public void setAD_Client_ID(int AD_Client_ID) {
		this.AD_Client_ID = AD_Client_ID;
	}

	public int getAD_Client_ID() {
		return AD_Client_ID;
	}
	
	public void setAD_User_ID(int AD_User_ID) {
		this.AD_User_ID = AD_User_ID;
	}

	public int getAD_User_ID() {
		return AD_User_ID;
	}	
	
}
